package com.glory.dgcpcpda.activity.mfg;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.ToolLifeListAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.TempRecipeEntity;
import com.glory.dgcpcpda.entity.ToolLifeListEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.List;

import butterknife.BindView;

/**
 * FileName: ToolLifeUpdateActivity
 * Author: XJH
 * Date: 2021/8/4 14:46
 * Description:
 * History:
 */
public class ToolLifeUpdateActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqp)
    FreeUI_ClearEditText etEqp;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.list_tv5)
    TextView listTv5;
    @BindView(R.id.rvToolList)
    RecyclerView rvToolList;
    @BindView(R.id.tvDownLoad)
    TextView tvDownLoad;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private ToolLifeListAdapter mAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_tool_life_update;
    }

    @Override
    protected void initView() {
        tvTitle.setText("修改治具寿命");
        setMainLayout(llMain);
        tvBaseSubmit.setVisibility(View.GONE);
        listTv1.setText("物料/治具名称");
        listTv2.setText("物料/治具批号");
        listTv3.setText("站点");
        listTv4.setText("当前寿命");
        listTv5.setText("修改寿命");
        rvToolList.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ToolLifeListAdapter(null);
        rvToolList.setAdapter(mAdapter);

        initEdit(etEqp, new editTextListener() {
            @Override
            public void onTextChangeListener(String s) {

                FreeUI_EntityCallBack<BaseResponseBean<ToolLifeListEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<ToolLifeListEntity>>
                        (new TypeToken<BaseResponseBean<ToolLifeListEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<ToolLifeListEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            mAdapter.setNewData(response.body().getResponse().getBody().getTOOLLIST());
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<ToolLifeListEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "TOOLREPORT");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE","LOAD");
                map.addProperty("EQUIPMENTID",s);
                NetUtils.requestPostNet("CPC.TOOLREPORT","TOOLQuery",this,map,callBack);
            }
        });
    }

    @Override
    protected void initData() {
        tvDownLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (getText(etEqp).isEmpty()){
                    showToast("请先扫描批次号",false);
                    return;
                }
                FreeUI_EntityCallBack<BaseResponseBean<ToolLifeListEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<ToolLifeListEntity>>
                        (new TypeToken<BaseResponseBean<ToolLifeListEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<ToolLifeListEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            showConfirmDialog(ToolLifeUpdateActivity.this,"修改成功！");
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<ToolLifeListEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "TOOLREPORT");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE","UPDATE");
                map.addProperty("EQUIPMENTID",getText(etEqp));
                map.add("TOOLLIST", new JsonParser().parse(new Gson().toJson(mAdapter.getData())).getAsJsonArray());
                NetUtils.requestPostNet("CPC.TOOLREPORT","TOOLLimitLifeChange",this,map,callBack);
            }
        });
    }
}
